﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeTester
{
    /// <summary>
    /// 单链表结点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingleLinkedListNode<T>
    {
        //结点数据
        public T Item { get; set; }
        //结点指针 单链表指点的是下一结点
        public SingleLinkedListNode<T> Next { get; set; }

        public SingleLinkedListNode() { }
        public SingleLinkedListNode(T item)
        {
            this.Item=item;
        }
    }

    public class SingleLinkedList<T>
    {
        private int count; // 字段：当前链表节点个数
        private SingleLinkedListNode<T> head; // 字段：当前链表的头结点

        public int Count
        {
            get
            {
                return this.count;
            }
        }

        public T this[int index]
        {
            get
            {
                return this.GetNodeByIndex(index).Item;
            }
            set
            {
                this.GetNodeByIndex(index).Item = value;
            }
        }

        public SingleLinkedList()
        {
            this.count = 0;
            this.head = null;
        }

        // Method01:根据索引获取节点
        private SingleLinkedListNode<T> GetNodeByIndex(int index)
        {
            if (index < 0 || index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index", "索引超出范围");
            }

            SingleLinkedListNode<T> tempNode = this.head;
            for (int i = 0; i < index; i++)
            {
                tempNode = tempNode.Next;
            }

            return tempNode;
        }

        /// <summary>
        /// 在链表尾增加节点
        /// </summary>
        /// <param name="value"></param>
        public void Add(T value)
        {
            SingleLinkedListNode<T> tempNode = new SingleLinkedListNode<T>(value);
            if (this.head == null)
            {
                this.head = tempNode;
            }
            else
            {
                SingleLinkedListNode<T> preNode = this.GetNodeByIndex(this.count - 1);
                preNode.Next = tempNode;
            }
            this.count++;
        }

        /// <summary>
        /// 在指定位置插入节点
        /// </summary>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void Insert(int index,T value)
        {
            if (index < 0 || index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index", "索引超出范围");
            }
            SingleLinkedListNode<T> tempNode = new SingleLinkedListNode<T>(value);
            if (index == 0)
            {
                if (this.head == null)
                {
                    this.head = tempNode;
                }
                else
                {
                    tempNode.Next = this.head;
                    this.head = tempNode;
                }
            }
            else
            {
                SingleLinkedListNode<T> preNode = this.GetNodeByIndex(index-1);
                tempNode.Next = preNode.Next;
                preNode.Next = tempNode;

            }
            this.count++;
        }
        /// <summary>
        /// 删除指点位置节点
        /// </summary>
        /// <param name="index"></param>
        public void RemoveAt(int index)
        {
            if (index < 0 || index >= this.count)
            {
                throw new ArgumentOutOfRangeException("index", "索引超出范围");
            }
            if (index == 0)
            {
                this.head = this.head.Next;
            }
            else
            {
                SingleLinkedListNode<T> preNode = this.GetNodeByIndex(index - 1);
                SingleLinkedListNode<T> deleteNode = preNode.Next;

                preNode.Next = preNode.Next.Next;
                deleteNode = null;
            }

            this.count--;
        }
    }
}
